#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Git Stash Helpers ===
alias gits='git stash';
alias gitsp='git stash -p';
alias gitsu='git stash -u';
alias gitspop='git stash pop';

# === Git Clean Tags ===
function gitcleantags()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: gitcleantags <branch_to_keep> (Cleanup unrequired git tags)';
    echo '';
    return;
  fi;

  # Variables
  local branch_keep=${1};

  # Delete tags
  git tag | grep -v "${branch_keep}" | xargs git tag -d;
}

# === Git One Branch Keeper ===
function gitonebranch()
{
  # Usage: gitonebranch (Git remove non-default remote branches)

  # Fetch remotes
  local username;
  username=$(githubusername);
  git fetch "${username}";
  git fetch "${username}" refs/heads/*:refs/heads/*;
  git fetch "${username}" refs/tags/*:refs/tags/*;

  # Remove branches
  git branch -r | sed "s/${username}\/\(.*\)/\1/" \
                | grep -v "HEAD" \
                | grep -v "$(git rev-parse --abbrev-ref HEAD)" \
                | xargs -I {} sh -c "echo -n \" Removing branch {} : \"; git push ${username} :{};";

  # Remove tags
  git tag -l | xargs -I {} sh -c "echo -n \" Removing tag {} : \"; git push ${username} :{}; echo -n "  "; git tag -d {};";
}
